package cn.itsource.org.controller;

import cn.itsource.basic.exception.BusinessException;
import cn.itsource.basic.util.ExcelUtil;
import cn.itsource.basic.vo.ShopVo;
import cn.itsource.org.domain.ShopAuditLog;
import cn.itsource.org.service.IShopService;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.query.ShopQuery;
import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public IShopService shopService;

    /*echarts表绘制*/
    @GetMapping("/stateStatistics")
    public AjaxResult stateStatistics(){
        List<ShopVo> shopVos = shopService.loadStateStatistics();
        ArrayList<Integer> states = new ArrayList<>();
        ArrayList<Integer> nums = new ArrayList<>();

        for (ShopVo shopVo : shopVos) {
            states.add(shopVo.getState());
            nums.add(shopVo.getNum());
        }

        HashMap<String, ArrayList<Integer>> map = new HashMap<>();
        map.put("x",states);
        map.put("y",nums);
        return AjaxResult.me().setResultObj(map);
    }
    /*excel导出导入*/
    //导出
    @GetMapping("/export/excel")
    public void exportExcel(HttpServletResponse response){
        List<Shop> shops = shopService.queryAll();
        //List<?> list, String title, String sheetName, Class<?> pojoClass,String fileName,HttpServletResponse response
        ExcelUtil.exportExcel(shops,"店铺信息","店铺1",Shop.class,"店铺信息表.xlsx",true,response);
    }
    //导入
    @PostMapping("/import/excel")
    public void importExcel(@RequestPart MultipartFile file){
        //String filePath,Integer titleRows,Integer headerRows, Class<T> pojoClass
        List<Shop> shops = ExcelUtil.importExcel(file, 1, 1, Shop.class);
        shops.forEach(System.out::println);
    }


    /*店铺审核是否通过*/
//    /shop/audit/pass
    @PostMapping("/audit/pass")
    public AjaxResult pass(@RequestBody ShopAuditLog log){
        try {
            shopService.pass(log);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("审核通过操作失败");
        }
    }

//    /shop/audit/reject
@PostMapping("/audit/reject")
    public AjaxResult reject(@RequestBody ShopAuditLog log){
        try {
            shopService.reject(log);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("审核驳回操作失败");
        }
    }


    /*
    * 店铺入驻接口
    * */
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("店铺入驻失败");
        }
    }

    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.add(shop);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Shop get(@PathVariable("id")Long id)
    {
        return shopService.queryById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Shop> list(){

        return shopService.queryAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<Shop> queryPage(@RequestBody ShopQuery query)
    {
        return shopService.queryPage(query);
    }
}
